package com.pug.controller.hotel;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pug.bo.HotelCommentBo;
import com.pug.controller.BaseController;
import com.pug.mapper.hotel.HotelZanMapper;
import com.pug.pojo.KssUser;
import com.pug.service.hotel.HotelCommentServiceImpl;
import com.pug.service.user.UserServiceImpl;
import com.pug.vo.HotelCommentPageVo;
import com.pug.vo.HotelCommentVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Author: lqm
 * create: 2022/3/23 13:33
 * Lover: lqx
 *
 * @see
 */
@RestController
public class HotelCommentController extends BaseController {
    @Resource
    HotelZanMapper hotelZanMapper;
    @Resource
    UserServiceImpl userService;
    @Resource
    HotelCommentServiceImpl hotelCommentService;
    @Resource(name = "stringRedisTemplate")
    StringRedisTemplate redisTemplate;
    @Resource
    RedisTemplate<String,Integer> integerRedisTemplate;
    //用户评论保存
    @PostMapping("/hotel/comment/save")
    public HotelCommentBo saveComment(@RequestBody HotelCommentVo hotelCommentVo, HttpServletRequest request){
        String authUserId = request.getHeader("AuthUserId");
        KssUser kssUser = userService.getById(authUserId);
         return hotelCommentService.saveComment(hotelCommentVo, kssUser);
    }
    //加载评论信息
    @PostMapping("/hotel/comment/load")
    public Page<HotelCommentBo> loadHotelComment(@RequestBody HotelCommentPageVo hotelCommentPageVo) {
        return hotelCommentService.loadHotelComment(hotelCommentPageVo);
    }
    //用户回复保存
    //删除评论
    @PostMapping("/hotel/comment/del/{hotelId}/{commentId}")
    public boolean deleteComment(@PathVariable("hotelId")Long hotelId, @PathVariable("commentId") Long commentId,HttpServletRequest request){
        String authUserId = request.getHeader("AuthUserId");
        KssUser kssUser = userService.getById(authUserId);
        Long userId=kssUser.getId();
//        Long userId=1506532557851463681L;
        return hotelCommentService.deleteComment(userId,commentId);
    }
    //评论点赞
//    @EnableNoToken(true)
    @PostMapping("/hotel/comment/like/{commentId}")
    public int  like(@PathVariable("commentId") Long commentId,HttpServletRequest request){
        integerRedisTemplate.opsForHash().put("comment",commentId+"",1);
        //拿到用户信息
        String authUserId = request.getHeader("AuthUserId");
        KssUser kssUser=userService.getById(authUserId);
        Object o = integerRedisTemplate.opsForHash().get(commentId + "", kssUser.getId() + "");
//        这个地方得加一把锁   加个毛线锁，tmd，redis修改是单线程的
        synchronized (this){
            if(o==null){
                //增加
                int i = hotelZanMapper.deleteIfByUserIdAndCommentAId(kssUser.getId(), commentId);
                if(i==1) return 0;
                integerRedisTemplate.opsForHash().put(commentId+"",kssUser.getId()+"",1);
                return 1;
            }
            else{
                //删除
                integerRedisTemplate.opsForHash().delete(commentId+"",kssUser.getId()+"");
                return 0;
            }
        }
    }
    //撤销评论点赞
    @PostMapping("/hotel/comment/unlike/{commentId}")
    public int  unlike(@PathVariable("commentId") Long commentId,HttpServletRequest request){
        integerRedisTemplate.opsForHash().put("comment",commentId,1);
        //拿到用户信息
        String authUserId = request.getHeader("AuthUserId");
        KssUser kssUser = userService.getById(authUserId);
        Object o = integerRedisTemplate.opsForHash().get(commentId + "", kssUser.getId() + "");
        if(o==null){
            //如果redis中没有，就在mysql中查询  结果为1的话，代表该用户已经点赞了，现在在数据库中删除就好了
            int i = hotelZanMapper.deleteIfByUserIdAndCommentAId(kssUser.getId(), commentId);
            if(i==1) return 0;
            integerRedisTemplate.opsForHash().put(commentId+"",kssUser.getId()+"",1);
            return 1;
        }
        else{
            integerRedisTemplate.opsForHash().delete(commentId+"",kssUser.getId()+"");
            return 0;
        }
    }
}
